package cn.zcscloud.mall.service.impl;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.mbg.mapper.PmsBrandMapper;
import cn.zcscloud.mall.mbg.model.PmsBrand;
import cn.zcscloud.mall.service.BrandService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: BrandServiceImpl
 * Description:
 * date: 2022/4/1 14:26
 *
 * @author zjf95
 * @since JDK 1.8
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private PmsBrandMapper pmsBrandMapper;

    @Override
    public PageRequest page(String keyword, int pageNum, int pageSize) {

        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(keyword != null, PmsBrand::getName, keyword);
        queryWrapper.orderByDesc(PmsBrand::getSort);

        Page<PmsBrand> pmsBrandPage = new Page<>(pageNum, pageSize);

        pmsBrandMapper.selectPage(pmsBrandPage, queryWrapper);

        return PageRequest.init(pmsBrandPage);
    }

    /**
     * Description: 更新时的数据回显
     *
     * @author: zjf95
     * @date: 2022/4/1 16:59
     * @param:
     * @return:
     */
    @Override
    public PmsBrand findById(Long id) {

        PmsBrand pmsBrand = pmsBrandMapper.selectById(id);

        return pmsBrand;
    }

    @Override
    public void update(PmsBrand pmsBrand) {
        if (pmsBrand == null) {
            throw new RuntimeException("传输的数据为空");

        }
        pmsBrandMapper.updateById(pmsBrand);
    }

    @Override
    public void updateByIds(Integer[] ids, Integer factoryStatus) {
        List<Long> bids = Arrays.stream(ids).map(e -> Long.valueOf(e)).collect(Collectors.toList());

        //LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        //queryWrapper.eq(factoryStatus != null,PmsBrand::getFactoryStatus,factoryStatus);
        //queryWrapper.in(!bids.isEmpty(), PmsBrand::getId,bids);

        LambdaUpdateWrapper<PmsBrand> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(!bids.isEmpty(), PmsBrand::getId, bids)
                .set(factoryStatus != null, PmsBrand::getFactoryStatus, factoryStatus);

        pmsBrandMapper.update(null, updateWrapper);
    }

    @Override
    public void updateShow(Integer[] ids, Integer showStatus) {
        List<Long> bids = Arrays.stream(ids).map(Long::valueOf).collect(Collectors.toList());


        LambdaUpdateWrapper<PmsBrand> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(!bids.isEmpty(), PmsBrand::getId, bids)
                .set(showStatus != null, PmsBrand::getShowStatus, showStatus);

        pmsBrandMapper.update(null, updateWrapper);
    }

    @Override
    public void deleteById(Integer id) {
        pmsBrandMapper.deleteById(id);
    }

}
